<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label">AppId</label>
                <div class="layui-input-block">
                    <input type="text" name="wechat_appid" maxlength="18" class="layui-input">
                    <tip>微信公众号的AppID，wx开头18位长度，从公众平台【开发】/【基本配置】页面获取</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">AppSecret</label>
                <div class="layui-input-block">
                    <input type="text" name="wechat_appsecret" maxlength="32" class="layui-input">
                    <tip>微信公众号的密钥，32位长度，从公众平台【开发】/【基本配置】页面获取</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">AesKey</label>
                <div class="layui-input-block">
                    <input type="text" name="wechat_aeskey" maxlength="50" class="layui-input">
                    <tip>如果公众平台开启了消息加密，请填写公众平台所设置的加密密钥</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
    <div class="layuimini-main">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label">API</label>
                <div class="layui-input-block">
                    <input type="text" name="wechatapi_url" class="layui-input layui-input-readonly" readonly>
                    <a href="javascript:;" class="icon-copy"><i class="fa fa-clipboard"></i></a>
                    <tip>微信服务器推送消息处理接口，用于微信公众平台配置</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">Token</label>
                <div class="layui-input-block">
                    <input type="text" name="wechatapi_token" class="layui-input layui-input-readonly" readonly>
                    <a href="javascript:;" class="icon-copy"><i class="fa fa-clipboard"></i></a>
                    <tip>消息处理接口的验证token，用于微信公众平台配置接口时</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <a href="javascript:;" id="resetWechatApiTokenBtn"
                        class="layui-btn layui-btn-sm layui-btn-primary layui-border-blue">重置Token</a>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">IP</label>
                <div class="layui-input-block">
                    <input type="text" name="server_ip" class="layui-input layui-input-readonly" readonly>
                    <a href="javascript:;" class="icon-copy"><i class="fa fa-clipboard"></i></a>
                    <tip>本系统服务器IP，用于微信平台白名单配置等</tip>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use([], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            http = layui.http,
            form = layui.form;

        //复制
        $('.icon-copy').on('click', function () {
            var content = $(this).prev("input").val();
            input = document.createElement('textarea');
            input.style.position = 'absolute', input.style.left = '-100000px', input.style.width = '1px', input.style.height = '1px', input.innerText = content;
            document.body.appendChild(input), input.select(), setTimeout(function () {
                document.execCommand('Copy') ? layer.msg('复制成功', { time: 1000 }) : layer.msg('复制失败，请使用鼠标操作复制！', { time: 1000 });
                document.body.removeChild(input);
            }, 100);
        });

        //重置token
        $('#resetWechatApiTokenBtn').on('click', function () {
            http.ajaxPost('/admin/Setting/resetWechatApiToken', {}, function (data) {
                $("[name='wechatapi_token']").val(data);
                layer.msg('重置成功', { time: 1000 }, function () { });
            });
        });

        //拉取信息
        var oldData;
        http.ajaxPost('/admin/Setting/getWechat', {}, function (data) {
            oldData = data;
            $("[name='wechat_appid']").val(data.wechat_appid);
            $("[name='wechat_appsecret']").val(data.wechat_appsecret);
            $("[name='wechat_aeskey']").val(data.wechat_aeskey);
            $("[name='wechatapi_url']").val(data.wechatapi_url);
            $("[name='wechatapi_token']").val(data.wechatapi_token);
            $("[name='server_ip']").val(data.server_ip);
        });

        //监听提交
        form.on('submit(saveBtn)', function (formdata) {
            for (let field in formdata.field) {
                if (formdata.field[field] == oldData[field]) {
                    delete formdata.field[field];
                }
            }
            http.ajaxPost('/admin/Setting/saveWechat', formdata.field, function (data) {
                for (let field in formdata.field) {
                    oldData[field] = formdata.field[field];
                }
                layer.msg('保存成功', { time: 1000 }, function () { });
            });
        });
    });
</script>